package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IPermissionService permissionService;

    @ResponseBody
    @RequestMapping("/login")
    public JsonResult login(String username, String password) {
        Employee employee = employeeService.login(username, password);
        if (employee != null) {
            //存入session
            UserContext.setEmployee(employee);
            List<String> expressions = permissionService.queryByEmployeeId(employee.getId());
            //存入session
            UserContext.setExpressions(expressions);
            return new JsonResult(true, "登陆成功");
        } else {
            return new JsonResult(false, "用户名或者密码错误");
        }
    }

    @RequestMapping("/logout")
    public String logout() {
        UserContext.removeEmployee();
//        session.invalidate();  干掉session中所有的数据
        return "redirect:/static/login.html";
    }
    @RequestMapping("/updatePassword")
    public String updatePassword() {
        UserContext.removeEmployee();

//        session.invalidate();  干掉session中所有的数据
        return "redirect:/static/login.html";
    }
}
